require 'fileutils'
require 'qiniu'
require 'digest'
require 'openssl'
require 'base64'
require 'time'
require 'net/http'
require 'uri'
class PayrollApi < Grape::API
	WillPaginate.per_page = 10
	version 'v1', using: :path
	format :json
	default_format :json
	formatter :json, Grape::Formatter::Rabl
	resource :payroll do
		#### 新建帖子 ####
		desc '工资单处理'
		params do
			optional :payroll_detail_id, type: String, desc: ''
			optional :user_id, type: String, desc: ''
			optional :payroll_status, type: String, desc: ''
			optional :payroll_code, type: String, desc: ''
		end
		post 'deal_payroll' do
			if params[:user_id].nil? || params[:payroll_detail_id].nil? || params[:payroll_status].nil?
				{code: "400", msg: "参数错误"}
			else
				payroll_detail = PayrollDetail.find(params[:payroll_detail_id])
				payroll_detail.payroll_status = params[:payroll_status]
				payroll_detail.payroll_code = params[:payroll_code]
				payroll_detail.save
				{
					code: "0", 
					msg: "执行成功",
					payroll_status: payroll_detail.payroll_status
				}
			end
		end
		#### 查询工资表 ####
		desc '查询工资表'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: String, desc: '页码'
		end
		get 'payrolls' do
			if params[:user_id].nil? || params[:project_id].nil?
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				payrolls = Payroll.where(project_id: params[:project_id]).paginate(:page => page).order('created_at DESC')
				payroll_list = []
				payrolls.each do |pr|
					payroll = Hash.new
					payroll["id"] = pr.id
					payroll["payroll_name"] = pr.payroll_name
					payroll["created_at"] = pr.created_at.nil? ? "" : pr.created_at.strftime("%Y-%m-%d %H:%M:%S")
					payroll_list << payroll
				end
				count = payrolls.size
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					payroll_list: payroll_list,
					count: count,
					next_page: next_page
				}
			end
		end

		desc '查询工资单'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :project_id, type: String, desc: '项目ID'
			optional :payroll_id, type: String, desc: '工资单ID'
			optional :page, type: String, desc: '页码'
		end
		get 'payroll_details' do
			if params[:user_id].nil? || params[:project_id].nil? || params[:payroll_id].nil?
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				project_user = ProjectUser.where(project_id: params[:project_id], user_id: params[:user_id]).first
				if project_user.nil?
					{
						code: "0", 
						msg: "员工不在该项目内"
					}
				else
					if project_user.role == 1 || project_user.role == 2
						payroll_details = PayrollDetail.where(payroll_id: params[:payroll_id]).paginate(:page => page).order('created_at DESC')
						payroll_details_list = []
						payroll_details.each do |prd|
							project_user = ProjectUser.find(prd.project_user_id)
							user = User.find(project_user.user_id)
							payroll_detail = Hash.new
							payroll_detail["id"] = prd.id
							payroll_detail["payroll_details_content"] =  "工人姓名：" + user.name+ "," + "出勤天数：" +  prd.work_days.to_s +  "天，"+ "应发工资：" + prd.totalPayAmount.to_s+  "元"
							payroll_detail["payroll_detail_status"] = prd.payroll_status
							payroll_details_list << payroll_detail
						end
						count = payroll_details.size
						next_page = params[:page].to_i + 1
						{
							code: "0", 
							msg: "OK",
							payroll_list: payroll_details_list,
							count: count,
							next_page: next_page
						}
					else
						payroll_details = PayrollDetail.where(payroll_id: params[:payroll_id], project_user_id: project_user.id, payroll_status: 1).paginate(:page => page).order('created_at DESC')
						payroll_details_list = []
						payroll_details.each do |prd|
							user = User.find(project_user.user_id)
							payroll_detail = Hash.new
							payroll_detail["id"] = prd.id
							payroll_detail["payroll_details_content"] = "工人姓名：" + user.name+ "," + "出勤天数：" +  prd.work_days.to_s +  "天，"+ "应发工资：" + prd.totalPayAmount.to_s+  "元"
							payroll_detail["payroll_detail_status"] = prd.payroll_status
							payroll_details_list << payroll_detail
						end
						count = payroll_details.size
						next_page = params[:page].to_i + 1
						{
							code: "0", 
							msg: "OK",
							payroll_list: payroll_details_list,
							count: count,
							next_page: next_page
						}
					end
				end
				
			end
		end
	end
end